# 字典的定义
# 定义字面量:{key:value,key:value,...,key:value}——元素内容是 键值对
# 定义字典变量: 变量 = {key:value,key:value,...,key:value}
# 定义空字典:变量 = {}         变量 = dict()
# 字典的 key 值不可以重复

my_dict_1 = {"wanglihong":99,"zhoujielun":95,"linjunjie":77}
my_dict_2 = {}   # set()——是空集合
my_dict_3 = dict()

print(f"my_dict_1的内容是:{my_dict_1},类型是:{type(my_dict_1)}")
print(f"my_dict_2的内容是:{my_dict_2},类型是:{type(my_dict_2)}")
print(f"my_dict_3的内容是:{my_dict_3},类型是:{type(my_dict_3)}")

# 定义重复 key 的字典
my_dict_4 = {"wanglihong":99,"wanglihong":88,"zhoujielun":95,"linjunjie":77}
print(f"my_dict_4的内容是:{my_dict_4},类型是:{type(my_dict_4)}")

# 从字典中基于 key 获取value
score_1 =my_dict_1["wanglihong"]
print(f"score_1 = {score_1}")
score_2 =my_dict_1["zhoujielun"]
print(f"score_2 = {score_2}")

# 字典的嵌套
# 字典的 key 和 value 可以是任意数据类型(key 不可以为字典)
grade_dict = {
    "wanglihong":{
        "语文":77,
        "数学":66,
        "英语":33
    },
    "zhoujielun":{
        "语文":88,
        "数学":86,
        "英语":55
    },
    "linjunjie":{
        "语文":99,
        "数学":96,
        "英语":66
    }
}
grade_1 = grade_dict["wanglihong"]
print(f"grade_1 = {grade_1}")
grade_2 = grade_dict["wanglihong"]["数学"]
print(f"grade_2 = {grade_2}")

# 字典的常用操作
# 新增元素——语法:字典[key] = value,结果:key 值不存在,字典被修改，新增了元素
grade_dict["zhangxueyou"] = {"语文":66,"数学":76,"英语":44}
print(f"grade_dict = {grade_dict}")

# 更新元素——语法:字典[key] = value,结果:key 值存在,字典被修改，新增了元素
grade_3 = grade_dict["zhangxueyou"]
print(f"grade_3 = {grade_3}")
grade_dict["zhangxueyou"]["语文"] = 99
grade_4 = grade_dict["zhangxueyou"]
print(f"grade_4 = {grade_4}")

# 删除元素——语法:字典.pop(key), 结果:获得指定 key 的 value,同时字典被修改，指定 key 的数据被删除
del grade_dict["zhangxueyou"]["语文"]
grade_3 = grade_dict["zhangxueyou"]
print(f"grade_3 = {grade_3}")
grade_5 = grade_dict.pop("zhangxueyou")
print(f"grade_dict = {grade_dict},grade_5 = {grade_5}")

# 清空字典
grade_dict.clear()
print(f"grade_dict = {grade_dict}")

# 获取全部 key 的方法——语法:字典,keys, 结果:得到字典中的全部key
grade_dict_keys = grade_dict.keys()
print(f"grade_dict_keys = {grade_dict_keys}")
my_dict_keys = my_dict_1.keys()
print(f"my_dict_keys = {my_dict_keys}")

# 遍历字典
# 方式1:
for key in my_dict_keys:
    print(f"字典的 key 是:{key}")
    print(f"字典的 value 是:{my_dict_1[key]}")

# 方式2:
for key in my_dict_1:
    print(f"字典的 key 是:{key}")
    print(f"字典的 value 是:{my_dict_1[key]}")

# 统计字典内的元素数量
num = len(my_dict_1)
print(num)






